/**
 * @title FilterActivity.java
 * @package com.esun.cheetah.view.match.matchlist
 * @description 为比赛列表做本地过滤，
 * 						过滤原则：切换日期将导致过滤失效；
 * 									  只切换比分，赔率不会导致过滤失效；
 * 									  切换推荐，全部，联赛会导致过滤失效
 * @author liugan
 * @date 2012-3-27
 * @copyright 深圳市易讯天空
 */
package com.esun.cheetah.view.match.matchlist;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.esun.cheetah.R;
import com.esun.cheetah.biz.match.matchlist.FilterUtil;
import com.esun.cheetah.view.custom.GridViewNoScroll;
import com.esun.cheetah.view.match.matchlist.adapter.FilterAdapter;
import com.esun.util.log.EDebug;

/**
 * @title FilterActivity
 * @description 过滤的Activity
 * @author liugan
 */
public class FilterActivity extends Activity {

	private GridViewNoScroll gvLeagues;// 联赛名多选框
	private LinearLayout llMatchState;
	private RadioGroup rgStates1, rgStates2;// 比赛状态单选
	private Button btnSelectAll;// 全选
	private Button btnSelectInverse;// 反选
	private Button btnSubmit;// 提交
	private Button btnCancel;// 取消

	private OnCheckedChangeListener listener1, listener2;

	/**
	 * @field adapter
	 * @description 多选框数据适配器
	 */
	private FilterAdapter adapter;

	/**
	 * @field state
	 * @description 比赛状态过滤标识
	 */
	private int state;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.filter_activity);
		// 控件域初始化
		initViewFields();
		// 如果不是今天，则隐藏比赛状态过滤
		hideStateIfNotToday();
		// 比赛状态单选框和联赛复选框初始化
		setLeaguesAndState();
		// 提交过滤
		btnSubmit.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				EDebug.o("选择了的联队：" + adapter.getSelectedLeagues().toString());
				// 更新过滤器中的选中的联赛及比赛状态
				FilterUtil.getInstance().getSelectedLeagueSet().clear();
				FilterUtil.getInstance().setSelectedLeagueSet(
						adapter.getSelectedLeagues());
				FilterUtil.getInstance().setState(state);
				// 开启过滤
				FilterUtil.getInstance().setNeedFilter(true);

				setResult(0, null);
				finish();
			}
		});

		// 取消
		btnCancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		// 全选
		btnSelectAll.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				adapter.selectAll();
				adapter.notifyDataSetChanged();
			}
		});

		// 反选
		btnSelectInverse.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				adapter.selectInverse();
				adapter.notifyDataSetChanged();
			}
		});

		listener1 = new RadioGroup.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.rb_state_filter_all:
					state = FilterUtil.STATE_ALL;
					break;
				case R.id.rb_state_filter_running:
					state = FilterUtil.STATE_RIGHT_NOW;
					break;

				default:
					break;
				}

				rgStates2.setOnCheckedChangeListener(null);
				rgStates2.clearCheck();
				rgStates2.setOnCheckedChangeListener(listener2);

				adapter.setAllLeagues(FilterUtil.getInstance()
						.getAllLeaguesAndChangeSelected(state));
				// 更新已选联赛
				adapter.setSelectedLeagues(FilterUtil.getInstance()
						.getSelectedLeagueSet());
				adapter.notifyDataSetChanged();
			}
		};

		listener2 = new RadioGroup.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.rb_state_filter_future:
					state = FilterUtil.STATE_FUTURE;
					break;
				case R.id.rb_state_filter_done:
					state = FilterUtil.STATE_DONE;
					break;
				default:
					break;
				}
				rgStates1.setOnCheckedChangeListener(null);
				rgStates1.clearCheck();
				rgStates1.setOnCheckedChangeListener(listener1);

				adapter.setAllLeagues(FilterUtil.getInstance()
						.getAllLeaguesAndChangeSelected(state));
				// 更新已选联赛
				adapter.setSelectedLeagues(FilterUtil.getInstance()
						.getSelectedLeagueSet());
				adapter.notifyDataSetChanged();
			}
		};

		// 比赛状态过滤
		rgStates1.setOnCheckedChangeListener(listener1);

		rgStates2.setOnCheckedChangeListener(listener2);
	}

	/**
	 * @title hideStateIfNotToday
	 * @description
	 */
	private void hideStateIfNotToday() {
		if (!FilterUtil.getInstance().isToday()) {
			TextView title = (TextView) findViewById(R.id.tv_filter_title_state_filter);
			title.setVisibility(View.GONE);
			llMatchState.setVisibility(View.GONE);
		}

	}

	/**
	 * @title setFilterDataAndState
	 * @description 设置联赛复选框及状态单选框
	 */
	private void setLeaguesAndState() {
		// 所有可选的联赛
		String[] allLeagues;
		int state = FilterUtil.STATE_ALL;
		if (FilterUtil.getInstance().isNeedFilter()) {
			allLeagues = FilterUtil.getInstance().getAllLeagues();
			state = FilterUtil.getInstance().getState();
		} else {
			allLeagues = FilterUtil.getInstance()
					.getAllLeaguesAndChangeSelected(FilterUtil.STATE_ALL);
		}
		adapter = new FilterAdapter(this);

		adapter.setAllLeagues(allLeagues);
		adapter.setSelectedLeagues(FilterUtil.getInstance()
				.getSelectedLeagueSet());
		gvLeagues.setAdapter(adapter);

		// 设置RadioGroup状态：全选，进行中，未开赛，完场
		if (FilterUtil.getInstance().isNeedFilter()) {// 已设置了过滤
			switch (state) {
			case FilterUtil.STATE_ALL:
				rgStates1.check(R.id.rb_state_filter_all);
				state = FilterUtil.STATE_ALL;
				break;
			case FilterUtil.STATE_RIGHT_NOW:
				rgStates1.check(R.id.rb_state_filter_running);
				state = FilterUtil.STATE_RIGHT_NOW;
				break;
			case FilterUtil.STATE_FUTURE:
				rgStates2.check(R.id.rb_state_filter_future);
				state = FilterUtil.STATE_FUTURE;
				break;
			case FilterUtil.STATE_DONE:
				rgStates2.check(R.id.rb_state_filter_done);
				state = FilterUtil.STATE_DONE;
				break;
			}
		} else {// 还未设置过滤
			rgStates1.check(R.id.rb_state_filter_all);
			state = FilterUtil.STATE_ALL;
		}

	}

	/**
	 * @title initViewFields
	 * @description 初始化控件fields
	 */
	private void initViewFields() {
		rgStates1 = (RadioGroup) findViewById(R.id.rg_filter_state_1);
		rgStates2 = (RadioGroup) findViewById(R.id.rg_filter_state_2);
		llMatchState = (LinearLayout) findViewById(R.id.ll_filter_radio_groups);
		btnSelectAll = (Button) findViewById(R.id.btn_filter_select_all);
		btnSelectInverse = (Button) findViewById(R.id.btn_filter_select_inverse);
		gvLeagues = (GridViewNoScroll) findViewById(R.id.gv_filter_matchs);
		btnSubmit = (Button) findViewById(R.id.btn_filter_submit);
		btnCancel = (Button) findViewById(R.id.btn_filter_cancel);
	}

}
